package com.pds.web.exit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.web.exit.entity.PdsOrganizationReset;
import com.pds.web.exit.mapper.PdsOrganizationResetMapper;
import com.pds.web.exit.service.PdsOrganizationResetService;
import com.pds.web.testOrganizationInfo.domain.PdsTestOrganizationInfo;
import com.pds.web.testOrganizationInfo.mapper.PdsTestOrganizationInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 机构重置详情表
 *
 * @author srf sunlightcs@gmail.com
 * @since 1.0.0 2024-02-06
 */
@Service("pdsOrganizationResetService")
public class PdsOrganizationResetServiceImpl extends ServiceImpl<PdsOrganizationResetMapper, PdsOrganizationReset> implements PdsOrganizationResetService {
    @Resource
    private PdsOrganizationResetMapper pdsOrganizationResetMapper;

    @Resource
    private PdsTestOrganizationInfoMapper pdsTestOrganizationInfoMapper;

    @Override
    public List<PdsOrganizationReset> selectList(PdsOrganizationReset pdsOrganizationReset) {
        QueryWrapper<PdsOrganizationReset> wrapper = new QueryWrapper<>();
        Date resetDate = pdsOrganizationReset.getResetDate();
        String resetReason = pdsOrganizationReset.getResetReason();
        if(!Objects.isNull(resetDate)){
            wrapper.eq("reset_date",resetDate);
        }
        if(!Objects.isNull(resetReason) && !resetReason.isEmpty()){
            wrapper.like("reset_reason",resetReason);
        }
        return pdsOrganizationResetMapper.selectList(wrapper);
    }

    @Override
    public PdsOrganizationReset queryById(Long id) {
        return pdsOrganizationResetMapper.selectById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public PdsOrganizationReset insert(PdsOrganizationReset pdsOrganizationReset) {
        Long organizationId = pdsOrganizationReset.getOrganizationId();
        if(!Objects.isNull(organizationId)){
            testOrganizationInfoReset(organizationId);
            Date date = DateUtils.parseDate(DateUtils.getTime());
            /*String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
            pdsOrganizationReset.setCreateBy(nickName);*/
            pdsOrganizationReset.setCreateTime(date);
            pdsOrganizationResetMapper.insert(pdsOrganizationReset);
        }
        return pdsOrganizationReset;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean update(PdsOrganizationReset pdsOrganizationReset) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        /*String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        pdsOrganizationReset.setUpdateBy(nickName);*/
        pdsOrganizationReset.setUpdateTime(date);
        return pdsOrganizationResetMapper.updateById(pdsOrganizationReset) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        return pdsOrganizationResetMapper.deleteById(id) > 0;
    }

    private void testOrganizationInfoReset(Long organizationId){
        PdsTestOrganizationInfo pdsTestOrganizationInfo = new PdsTestOrganizationInfo();
        pdsTestOrganizationInfo.setId(organizationId);
        pdsTestOrganizationInfo.setStatus("5");
        pdsTestOrganizationInfoMapper.updateById(pdsTestOrganizationInfo);
    }
}